package cn.gson.crm.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.gson.crm.model.entity.Classroom;

public class ClassroomDao {
	
	/**
	 * 获得所有班级信息
	 * @return
	 * @throws SQLException
	 */
	public List<Classroom> getListClass() throws SQLException{
		String sql = "select * from class";
		 List<Map<String, Object>> data = Jdbc.getInstace().executeQueryForList(sql);
		 
		 List<Classroom> classroom = null;
		if(data != null){
			classroom= new ArrayList<>();
			for (Map<String, Object> map : data) {
				classroom.add(buildClassroom(map));
			}
		}
		return classroom;
	}
	
	/**
	 * 保存一个班级
	 * @param cr
	 * @return
	 * @throws SQLException
	 */
	public boolean classroomSave(Classroom cr) throws SQLException{
		// 新建
		String sql = "insert into class(class_name,class_manager,class_teacher,class_type) values(?,?,?,?)";
		return Jdbc.getInstace().executeUpdate(sql, cr.getClassName(),cr.getClassManager(),cr.getClassTeacher(),cr.getClassType());
	}
	/**
	 * 获得所有班级信息，可以分快班和慢班
	 * @param classType
	 * @return
	 * @throws SQLException
	 */
	public List<Classroom> getListClass(Integer classType) throws SQLException{
		String sql = "select * from class where class_type = ?";
		 List<Map<String, Object>> data = Jdbc.getInstace().executeQueryForList(sql, classType);
		 
		 List<Classroom> classroom = null;
		if(data != null){
			classroom= new ArrayList<>();
			for (Map<String, Object> map : data) {
				classroom.add(buildClassroom(map));
			}
		}
		return classroom;
	}
	
	/**
	 * 分配给班级时通过学生的Id修改学生的班级Id
	 * @param classId
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public boolean allocationClassByClassId(Integer classId, Integer classType,Integer id) throws SQLException{
		String sql = "update student set class_id = ? ,class_type=?, grade_id = 6 where id = ? ";
		return Jdbc.getInstace().executeUpdate(sql, classId, classType,id);
	}
	
	private Classroom  buildClassroom(Map<String, Object> map){
		Classroom cr = new Classroom();
		if(map.get("id").toString() != null){
			
		}
		cr.setId(Integer.parseInt(map.get("id").toString()));
		if(map.get("class_name") != null){
			cr.setClassName((String)map.get("class_name"));	
		}
		
		cr.setClassManager((String)map.get("class_manager"));
		cr.setClassTeacher((String)map.get("class_teacher"));
		cr.setClassType(Integer.parseInt(map.get("class_type").toString()));
		cr.setClassCreateTime((Date)map.get("class_create_time"));
		
		return cr;
	}
}
